home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Constructor Exceptions
- Date: 8 Jan 1996 15:18:32 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4crck8$9oh@dawn.mmm.com>
- References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce68n$8u4@sundog.tiac.net> <4cglh3$6ue@oxy.rust.net>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Paul Gunn (pgunn@mail.cbf.com) wrote:
- > page@tiac.net (Chris Page) wrote:
-
-
- > >Sure, they'll clean up 'this', but the programmer is responsible for
- > >any and all heap memory allocated within the body of constructor.
-
- > My impression was that they did not. I'll try to find the article I
- > read -- perhaps I misunderstood the point.
-
- > >The solution to this problem is to use the "resource allocation is
- > >class initialization" technique, which has been discussed in the news
- > >group many time before.
-
- > Could you give a brief synopsis of this technique? I may be familiar
- > with it, but not by name.
-
- This term is used to describe classes which acquire resources in their
- constructors and release them in their destructors. (More loosely
- applied, objects of those kinds of classes may not actually acquire
- the resources, but may be used to hold on to them for release in their
- destructors.)
-
- One example is the ofstream class. Given a name, it opens the file.
- When the ofstream is destroyed, it will close the file.
-
- Another example of this is auto_ptr<T>, which holds a pointer to a
- dynamically allocated T. The destructor of auto_ptr<T> deletes the
- pointer. This is useful for holding onto memory that is no longer
- needed outside a certain block. You can give the pointer to an
- auto_ptr<T> and rest assured that the memory will be released no matter
- how you exit the block.
-
- See also section 16.5 of "The Design and Evolution of C++," by Bjarne
- Stroustrup.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-